﻿@{
    ViewBag.Title = ViewData["listtitle"];
    Layout = "~/Views/Shared/_Layout2.cshtml";
    var cul = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
    //var titlefilter = Request.Params["paramtitle"];
    //var locationfilter = Request.Params["paramlocation"];
    //var categoryfilter = Request.Params["paramcategory"];
    var cate = ViewData["selectcate"];
    var local = ViewData["selectlocal"] as string;
    var filter = ViewData["selectfilter"] ?? "";
    var listCate = ViewData["AllCate"] as List<SmallJob.Model.Category>;
}
<form class="container form-inline" style="margin-top: 50px;">
    <div id="filter-detail-content">
        <select name="location" id="filter-location" onchange="triggerfilternormal()">
            <option value="0" selected="@(string.IsNullOrWhiteSpace(local) ? "selected" : "")">Tất cả Địa Điểm</option>
            @foreach (var item in SmallShop.Services.Implements.UtilityService.GetCities())
            {
                if (local != null && local.Equals(item.Name))
                {
                <option value="@item.Name" selected="selected">@item.Name</option>
                }
                else
                {
                <option value="@item.Name">@item.Name</option>
                }
            }
        </select>
        <select name="cateID" id="filter-category" onchange="triggerfilternormal()">
            <option value="0" selected="@(cate == null ? "selected" : "")">Tất cả Danh mục</option>
            @foreach (var item in listCate)
            {
                if (cate != null && cate.ToString().Equals(item.ID.ToString()))
                {
                <option value="@item.ID" selected="selected">@item.Name</option>
                }
                else
                {
                <option value="@item.ID">@item.Name</option>
                }
            }
        </select>
        <select name="price" id="filter-price" onchange="triggerfilternormal()">
            <option value="0" selected="@(cate == null ? "selected" : "")">Tất cả Mức giá</option>
            <option value="1">Dưới 500.000</option>
            <option value="2">500.000 - 1 triệu</option>
            <option value="3">1 triệu - 2 triệu</option>
            <option value="4">2 triệu - 5 triệu</option>
            <option value="5">Trên 5 triệu</option>
        </select>
        <input type="button" value="Xóa bộ lọc" class="btn" onclick="resetfilter()" />
    </div>
</form>
<div class="listing-services">
    <div class="container">
        @if (ViewData["provides"] != null)
        {
            <h2>@ViewData["listtitle"]</h2>
            
            <div class="row">
                @foreach (var item in ViewData["provides"] as List<SmallJob.Model.Provide>)
                {
                    string[] image = item.ImagePath.Split(';');
                    string url = image[0] + "?w=368&h=228&q=90&format=jpg&mode=crop";
                    <div class="span4">
                        <div class="service-box">
                            <div class="service-image">
                                <a href="@Url.Action("Details", "Provide", new { id = item.ID })">
                                    <img src="@url"/></a>
                            </div>
                            <div class="service-description">
                                <div class="avatar">
                                    <a href="@Url.Action("Index", "User", new { username = (string.IsNullOrWhiteSpace(item.User.UserName) ? item.User.ID.ToString() : item.User.UserName) })">
                                        @{if (string.IsNullOrWhiteSpace(item.User.Avatar))
                                          {
                                            <img rel="tooltip" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="@item.User.ProperName" src="@Url.Content("~/Content/img/blank-avatar.jpg")"/>
                                          }
                                          else
                                          {
                                            <img rel="tooltip" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="@item.User.ProperName" src="@item.User.Avatar?w=40&h=40&q=90&format=jpg&mode=crop"/>
                                          }
                                        }
                                    </a>
                                </div>
                                <div class="service-title">
                                    <a href="@Url.Action("Details", "Provide", new { id = item.ID })">
                                        <h5>@item.Title</h5>
                                    </a>
                                </div>
                                <div class="service-price">
                                    <a href="#" rel="tooltip" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="@item.Price.ToString("#,###", cul.NumberFormat)  VNĐ" style="text-decoration:none;">@SmallJob.Services.Code.UserUtility.FormatCurrency(item.Price)</a>
                                </div>
                            </div>
                        </div>
                    </div>
                }
            </div>
        }

    </div>
</div>
@using (Html.BeginForm("Show", "Provide", FormMethod.Post, new { id = "hidden-filter" }))
{
    <input type="hidden" name="page" value="0" />
    <input type="hidden" name="paramlocation" value="@ViewData["selectlocal"]"/>
    <input type="hidden" name="paramcategory" value="@ViewData["selectcate"]"/>
    <input type="hidden" name="paramtitle" value="@filter" />
    <input type="hidden" name="minprice" value="" />
    <input type="hidden" name="maxprice" value="" />
}
<script>
    var originlocal = '@MvcHtmlString.Create(local)';
    var origincate = '@cate';
    function resetfilter() {
        if (originlocal == '') {
            $('#filter-location').val(0);
        } else {
            $('#filter-location').val(originlocal);
        }
        if (origincate == '') {
            $('#filter-category').val(0);
        } else {
            $('#filter-category').val(origincate);
        }
        $('#filter-price').val(0);
        triggerfilternormal();
    }
    function getminprice(idvalue) {
        idvalue = parseInt(idvalue);
        switch (idvalue) {
            case 0: return '';
            case 1: return 500000;
            case 2: return 500000;
            case 3: return 1000000;
            case 4: return 2000000;
            case 5: return '';
            default: return '';
        }
    }
    function getmaxprice(idvalue) {
        idvalue = parseInt(idvalue);
        switch (idvalue) {
            case 0: return '';
            case 1: return '';
            case 2: return 1000000;
            case 3: return 2000000;
            case 4: return 5000000;
            case 5: return 5000000;
            default: return '';
        }
    }
    function collectdata() {

        var form = $('#hidden-filter');
        var location = $('#filter-location').val();
        if (location != '0') {
            $(form).find('[name=paramlocation]').val(location);
        } else {
            $(form).find('[name=paramlocation]').val('');
        }

        var category = $('#filter-category').val();
        if (category != '0') {
            $(form).find('[name=paramcategory]').val(category);
        } else {
            $(form).find('[name=paramcategory]').val('');
        }

        //var title = $('#filter-title').val();
        //$(form).find('[name=paramtitle]').val(title);

        var idvalue = $('#filter-price').val();
        $(form).find('[name=minprice]').val(getminprice(idvalue));
        $(form).find('[name=maxprice]').val(getmaxprice(idvalue));
        return true;
    }
    function triggerfilternormal() {
        console.log('collect');
        $('#hidden-filter').find('[name=page]').val(1);
        if (!collectdata()) {
            return;
        }
        $.get("/Provide/Show", $('#hidden-filter').serialize(), function (data) {
            var getdata = $(data).find('.container').find('.row').html();
            $('.container').find('.row').html(getdata);
        })
    }
</script>
